package com.uniriho.lxr0860_printer.print;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
import com.uniriho.lxr0860_printer.AppCacheUtils;
import com.uniriho.lxr0860_printer.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TemplateActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template);
        findViewById(R.id.btnPrint).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String state = AppCacheUtils.getFile("template.pdf").getAbsolutePath();
                String state_src = AppCacheUtils.getFile("template_src.pdf").getAbsolutePath();
                String state_dest = AppCacheUtils.getFile("template_dest.pdf").getAbsolutePath();
                Font font = FontFactory.getFont(PdfUtil.FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                File src = new File(state_src);
                File dest = new File(state_dest);
                if (src.exists())
                    src.delete();

                if (dest.exists()) {
                    dest.delete();
                }
                Document document = new Document();
                PdfWriter writer = null;
                try {
                    writer = PdfWriter.getInstance(document, new FileOutputStream(state_src));

                    writer.setPageEvent(new FieldChunk());
                    document.open();
                    Paragraph pName = new Paragraph("公司名称：", font);
                    Chunk name = new Chunk("                   ");
                    name.setGenericTag("name");
                    pName.add(name);

                    Paragraph pAddress = new Paragraph("公司地址：", font);
                    Chunk address = new Chunk("                  ");
                    address.setGenericTag("address");
                    pAddress.add(address);

                    Paragraph pCulture = new Paragraph("公司文化：", font);
                    Chunk culture = new Chunk("                  ");
                    address.setGenericTag("culture");
                    pCulture.add(culture);


                    document.add(pName);
                    document.add(pAddress);
                    document.add(pCulture);

                    document.close();


                    PdfUtil.write(state_src, dest);
                    PdfUtil.print(state_dest);
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public class FieldChunk extends PdfPageEventHelper {
        @Override
        public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
            TextField field = new TextField(writer, rect, text);
            try {
                writer.addAnnotation(field.getTextField());
            } catch (IOException ex) {
                throw new ExceptionConverter(ex);
            } catch (DocumentException ex) {
                throw new ExceptionConverter(ex);
            }
        }
    }


}
